package com.xlink.action;

import com.xlink.dto.Datapoint;
import com.xlink.service.DatapointService;
import org.moon.base.action.BaseAction;
import org.moon.message.WebResponse;
import org.moon.rbac.domain.annotation.MenuMapping;
import org.moon.rest.annotation.Delete;
import org.moon.rest.annotation.Get;
import org.moon.rest.annotation.Post;
import org.moon.rest.annotation.Put;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * Created by jk on 2015/12/22.
 */
@Controller("DatapointAction")//控制器标示
@RequestMapping("/datapoint")//映射路径转发,对于该类中的所有方法，访问时必须有/datapoint前缀
public class DatapointAction extends BaseAction {
    //通过Spring容器注入服务类

    @Qualifier("datapointServiceImpl")
    @Resource
    private DatapointService datapointService;
    //这里我们在示例中需要添加数据点的增加、删除、修改、查询四种业务逻辑方法
    //首先需要一个前台的操作页面的转发方法

    @Get("")//表示当前台请求/datapoint时
    @MenuMapping(code="datapoint",name="数据点",parentCode="test",url="/datapoint")//此为菜单映射标示,表示将会在test对应的一级菜单下添加一个为学生信息的菜单，并且映射到/student上
    public ModelAndView showPage(){
        return new ModelAndView("pages/xlink/datapoint");
    }

    //@ResponseBody表示直接返回结果集，而无需映射到页面

    //添加新增逻辑代码
    @Post("{sensorId}")
    public @ResponseBody
    WebResponse add(HttpServletRequest request, @RequestParam("datapointId")Long datapointId, @RequestParam("datapointValue")Object datapointValue, @PathVariable("sensorId")Long sensorId, @RequestParam("API_Key")String API_Key) {
        Datapoint datapoint=new Datapoint();
        datapoint.setDatapointId(datapointId);
        datapoint.setDatapointValue(datapointValue);
        datapoint.setSensorId(sensorId);
        datapoint.setAPI_Key(API_Key);
        return WebResponse.build().setSuccess(datapointService.add(datapoint));
    }

    @Delete("{sensorId}/{datapointId}")
    public @ResponseBody
    WebResponse delete(HttpServletRequest request, @PathVariable("datapointId")Long datapointId, @PathVariable("sensorId")Long sensorId, @RequestParam("API_Key")String API_Key) {
        Datapoint datapoint=new Datapoint();
        datapoint.setSensorId(sensorId);
        datapoint.setDatapointId(datapointId);
        return WebResponse.success(datapointService.delete(datapoint));
    }

    //然后添加更新逻辑
    @Put("{sensorId}/{datapointId}")   //修改
    public @ResponseBody
    WebResponse update(HttpServletRequest request, @PathVariable("datapointId")Long datapointId, @RequestParam("datapointValue")Object datapointValue, @PathVariable("sensorId")Long sensorId, @RequestParam("API_Key")String API_Key) {
        Datapoint datapoint=new Datapoint();
        datapoint.setDatapointId(datapointId);
        datapoint.setDatapointValue(datapointValue);
        datapoint.setSensorId(sensorId);
        datapoint.setAPI_Key(API_Key);
        return WebResponse.success(datapointService.update(datapoint));
    }

    //获取单个数据信息
    @Get("{sensorId}/{datapointId}")
    public @ResponseBody
    WebResponse get(HttpServletRequest request, @PathVariable("datapointId")Long datapointId, @PathVariable("sensorId")Long sensorId){
        Datapoint datapoint=new Datapoint();
        datapoint.setDatapointId(datapointId);
        datapoint.setSensorId(sensorId);
        //datapoint.setAPI_Key(API_Key);
        return WebResponse.success(datapointService.get(datapoint));
    }

    //获取数据列表
    @Get("list/{sensorId}")
    @ResponseBody
    public WebResponse list(HttpServletRequest request,
                            @PathVariable("sensorId")Long sensorId,
                            @RequestParam("API_Key")String API_Key,
                            @RequestParam("displayTime")Long displayTime){
        Datapoint datapoint=new Datapoint();
        datapoint.setSensorId(sensorId);
        datapoint.setAPI_Key(API_Key);
        datapoint.setDisplayTime(displayTime);
        return WebResponse.success(datapointService.list(datapoint));
    }

    @Get("{sensorId}")
    @ResponseBody
    public WebResponse getnew(HttpServletRequest request,
                              @PathVariable("sensorId")Long sensorId,
                              @RequestParam("API_Key")String API_Key){
        Datapoint datapoint=new Datapoint();
        datapoint.setAPI_Key(API_Key);
        datapoint.setSensorId(sensorId);
        return WebResponse.success(datapointService.getnew(datapoint));
    }

}
